<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { 
    categoryListService, 
    categoryAddService, 
    categoryUpdateService,
    categoryDeleteService 
} from '@/api/category.js'
import { ElMessage, ElMessageBox } from 'element-plus'

const categorys = ref([])
const dialogVisible = ref(false)
const title = ref('')

const categoryModel = ref({
    categoryName: '',
    categoryAlias: '',
    sortOrder: 0,
    status: 1
})

const rules = {
    categoryName: [
        { required: true, message: '请输入分类名称', trigger: 'blur' },
    ],
    categoryAlias: [
        { required: true, message: '请输入分类别名', trigger: 'blur' },
    ]
}

const loadCategories = async () => {
    try {
        const { data } = await categoryListService()
        categorys.value = data
    } catch (error) {
        ElMessage.error(error.response?.data?.message || '获取分类列表失败')
    }
}

const addCategory = async () => {
    try {
        await categoryAddService(categoryModel.value)
        ElMessage.success('添加分类成功')
        loadCategories()
    } catch (error) {
        ElMessage.error(error.response?.data?.message || '添加分类失败')
    }
    dialogVisible.value = false;
}

const showDialog = (row) => {
    dialogVisible.value = true; title.value = '编辑分类'
    categoryModel.value.categoryName = row.categoryName;
    categoryModel.value.categoryAlias = row.categoryAlias;
    categoryModel.value.id = row.id
}

const updateCategory = async () => {
    try {
        await categoryUpdateService(categoryModel.value)
        ElMessage.success('更新分类成功')
        loadCategories()
    } catch (error) {
        ElMessage.error(error.response?.data?.message || '更新分类失败')
    }
    dialogVisible.value = false;
}

const clearData = () => {
    categoryModel.value.categoryName = '';
    categoryModel.value.categoryAlias = '';
}

const deleteCategory = (row) => {
    ElMessageBox.confirm(
        '你确认要删除该分类信息吗?',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
    .then(async () => {
        await categoryDeleteService(row.id)
        ElMessage.success('删除分类成功')
        loadCategories()
    })
    .catch(() => {
        ElMessage({
            type: 'info',
            message: '用户取消了删除',
        })
    })
}

loadCategories()
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>文章分类</span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible = true; title = '添加分类'; clearData()">添加分类</el-button>
                </div>
            </div>
        </template>
        <el-table :data="categorys" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <el-table-column label="分类名称" prop="categoryName" width="150"></el-table-column>
            <el-table-column label="分类别名" prop="categoryAlias" width="150"></el-table-column>
            <el-table-column label="排序" prop="sortOrder" width="100" align="center"></el-table-column>
            <el-table-column label="状态" width="100" align="center">
                <template #default="{ row }">
                    <el-tag :type="row.status === 1 ? 'success' : 'danger'">
                        {{ row.status === 1 ? '启用' : '禁用' }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteCategory(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>

        <!-- 添加分类弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-form :model="categoryModel" :rules="rules" label-width="100px" style="padding-right: 30px">
                <el-form-item label="分类名称" prop="categoryName">
                    <el-input v-model="categoryModel.categoryName" minlength="1" maxlength="50"></el-input>
                </el-form-item>
                <el-form-item label="分类别名" prop="categoryAlias">
                    <el-input v-model="categoryModel.categoryAlias" minlength="1" maxlength="50"></el-input>
                </el-form-item>
                <el-form-item label="排序值" prop="sortOrder">
                    <el-input-number v-model="categoryModel.sortOrder" :min="0" :max="999" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="categoryModel.status">
                        <el-radio :label="1">启用</el-radio>
                        <el-radio :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title == '添加分类' ? addCategory() : updateCategory()"> 确认 </el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>
